﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAnt.Core;
using System.Collections;
using System.Resources;
using NAnt.SVN.Tasks;

namespace NAnt.SVN.Util
{
    /// <summary>
    /// Get and set the properties in this family of tasks. This class
    /// prefixes the properties with 'svn' to ensure a unique proprtyname.
    /// $Id$
    /// </summary>
    sealed public class PropertiesManager : IPropertiesManager
    {
        #region Private Fields

        BaseExtensionTask task;

        #endregion

        #region Public Fields

        /// <summary>
        /// All th properties of this family of task are prefixed
        /// by this string
        /// </summary>
        public const string PROPERTYPREFIX = "svn";

        /// <summary>
        /// The task infrastructure
        /// </summary>
        public Task Task
        {
            get
            {
                return task;
            }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="task"></param>
        public PropertiesManager(BaseExtensionTask task)
        {
            this.task = task;
        }

        #region Interface Implementation

        /// <summary>
        /// Set a SVN property
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetSVNProperty(string key, object value)
        {
            SetProperty(ConvertToSVNKey(key), value);
        }

        /// <summary>
        /// Get a SVN property
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetSVNProperty(string key)
        {
            return GetProperty(ConvertToSVNKey(key));
        }

        /// <summary>
        /// Set a nant property
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetProperty(string key, object value)
        {
            if (IsProperty(key))
            {
                task.Properties[key] = value.ToString();
            }
            else
            {
                task.Properties.Add(key, value.ToString());
            }
        }

        /// <summary>
        /// Get a nant property
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetProperty(string key)
        {
            if (IsProperty(key))
            {
                return task.Properties[key];
            }
            else return string.Empty;
        }

        /// <summary>
        /// Does a svn property exist
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsSVNProperty(string key)
        {
            return IsProperty(ConvertToSVNKey(key));
        }

        public bool IsProperty(string key)
        {
            try {
                return task.Properties[key] != null;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// List SVN properties as a dictionary
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ListSVNProperties()
        {
            IDictionaryEnumerator _enum = task.Properties.GetEnumerator();
            Dictionary<string, string> _result = new Dictionary<string, string>();
            while (_enum.MoveNext())
            {
                string _key = _enum.Key.ToString();
                string _val = _enum.Value.ToString();
                if (_key.StartsWith(PROPERTYPREFIX + "."))
                {
                    _result.Add(_key, _val);
                }
            }
            return _result;
        }

        #endregion

        #region Utils

        /// <summary>
        /// Convert a key to a svn.* key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ConvertToSVNKey(string key)
        {
            return PROPERTYPREFIX + "." + key;
        }

        #endregion
    }
}
